package com.nbsaas.boot.controller.data;

import com.nbsaas.boot.code.annotation.api.QuerySqlApi;
import com.nbsaas.boot.code.annotation.domain.request.QueryRequest;
import com.nbsaas.boot.data.api.apis.QueryTemplateApi;
import com.nbsaas.boot.data.api.apis.QueryTemplateHistoryApi;
import com.nbsaas.boot.data.api.domain.request.QueryTemplateHistoryRequest;
import com.nbsaas.boot.data.api.domain.request.QueryTemplateRequest;
import com.nbsaas.boot.data.api.domain.request.QueryTemplateSearch;
import com.nbsaas.boot.data.api.domain.response.QueryTemplateResponse;
import com.nbsaas.boot.data.api.domain.simple.QueryTemplateSimple;
import com.nbsaas.boot.data.ext.domain.ext.QueryTemplateExt;
import com.nbsaas.boot.rest.annotations.*;
import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.rest.response.PageResponse;
import com.nbsaas.boot.rest.response.ResponseObject;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 *  控制器
 */
@RequiresAuthentication
@RestController
@RequestMapping("/queryTemplate")
public class QueryTemplateController {


    @Resource
    private QueryTemplateApi queryTemplateApi;

    @Resource
    private QueryTemplateHistoryApi queryTemplateHistoryApi;


    /**
     * 搜索数据
     *
     * @param request
     * @return
     */
    @SearchData
    @RequiresPermissions("queryTemplate")
    @PostMapping("/search")
    public PageResponse<QueryTemplateSimple> search(@RequestBody QueryTemplateSearch request) {
        return queryTemplateApi.search(request);
    }

    @SearchData
    @PostMapping("/query")
    public ResponseObject<String> getQuery(@RequestBody QueryRequest request) {

        QuerySqlApi querySqlApi = new QuerySqlApi() {
            @Override
            public ResponseObject<String> sql(QueryRequest request) {
                return QuerySqlApi.super.sql(request);
            }
        };
        return querySqlApi.sql(request);
    }

    /**
     * 列表数据
     *
     * @param request
     * @return
     */
    @SearchData
    @RequiresPermissions("queryTemplate")
    @PostMapping("/list")
    public ListResponse<QueryTemplateSimple> list(@RequestBody QueryTemplateSearch request) {
        return queryTemplateApi.list(request);
    }

    /**
     * 添加数据
     *
     * @param request
     * @return
     */
    @RecordLogData
    @RequiresPermissions("queryTemplate")
    @CreateData
    @PostMapping("/create")
    public ResponseObject<QueryTemplateResponse> create(@RequestBody @Validated(AddOperator.class) QueryTemplateRequest request) {
        ResponseObject<QueryTemplateResponse> result = new ResponseObject<>();
        if (request.getId() == null) {
            result.setCode(501);
            result.setMsg("模板已经存在");
            return result;
        }
        ResponseObject<QueryTemplateResponse> res = queryTemplateApi.view(request);
        if (res.getCode() == 200) {
            result.setCode(501);
            result.setMsg("模板已经存在");
            return result;
        }
        return queryTemplateApi.create(request);
    }

    /**
     * 修改数据
     * @param request
     * @return
     */
    @RecordLogData
    @RequiresPermissions("queryTemplate")
    @UpdateData
    @PostMapping("/update")
    public ResponseObject<QueryTemplateResponse> update(@RequestBody @Validated(UpdateOperator.class) QueryTemplateRequest request) {
        QueryTemplateHistoryRequest req = new QueryTemplateHistoryRequest();
        req.setQueryTemplate(request.getId());
        req.setContent(request.getContent());
        req.setAddDate(new Date());
        req.setCreator(request.getModifier());
        queryTemplateHistoryApi.create(req);
        return queryTemplateApi.update(request);
    }

    /**
     * 删除数据
     *
     * @param request
     * @return
     */
    @RecordLogData
    @SearchData
    @RequiresPermissions("queryTemplate")
    @PostMapping("/delete")
    public ResponseObject<?> delete(@RequestBody @Validated(DeleteOperator.class) QueryTemplateRequest request) {
        return queryTemplateApi.delete(request);
    }

    /**
     * 查看数据
     *
     * @param request
     * @return
     */
    @SearchData
    @RequiresPermissions("queryTemplate")
    @PostMapping("/view")
    public ResponseObject<QueryTemplateResponse> view(@RequestBody @Validated(ViewOperator.class) QueryTemplateRequest request) {
        return queryTemplateApi.view(request);
    }

    @PostMapping("/ext")
    public ResponseObject<QueryTemplateExt> ext(@RequestBody @Validated(ViewOperator.class) QueryTemplateRequest request) {
        ResponseObject<QueryTemplateExt> result = new ResponseObject<>();
        ResponseObject<QueryTemplateResponse> res = queryTemplateApi.view(request);
        if (res.getCode() != 200) {
            result.setCode(res.getCode());
            result.setMsg(res.getMsg());
            return result;
        }
        result.setData(res.getData().getExtData());
        return result;
    }
}